package com.xyrl.project.common.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyrl.project.common.common.enu.AttImageUseType;
import com.xyrl.project.common.common.service.AttachmentService;
import com.xyrl.project.common.common.vo.AttachmentVo;
import com.xyrl.project.common.constant.XyrlConstant;
import com.xyrl.project.common.store.entity.Store;
import com.xyrl.project.common.store.entity.StoreLegal;
import com.xyrl.project.common.store.dao.StoreLegalMapper;
import com.xyrl.project.common.store.enu.StoreStatus;
import com.xyrl.project.common.store.enu.StoreStatusNode;
import com.xyrl.project.common.store.service.StoreAuthService;
import com.xyrl.project.common.store.service.StoreLegalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyrl.project.common.store.service.StoreService;
import com.xyrl.starter.lock.redis.annotations.LockAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
* @className StoreLegalServiceImpl.java
* @company 重庆星纭融链科技公司
* @author 徐恩源
* @version 1.0
* @date 2019-12-31 14:08:40
* @Description 商户企业法人serviceImpl
*/
@Service
public class StoreLegalServiceImpl extends ServiceImpl<StoreLegalMapper, StoreLegal> implements StoreLegalService {
    @Autowired
    private StoreService storeService;
    @Autowired
    private AttachmentService attachmentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @LockAction(group = "locks:bindIdCard:", value = "#storeLegal.storeId", action = LockAction.LockFailAction.GIVEUP)
    public String bindIdCard(StoreLegal storeLegal, Long salesId, boolean isSalesUser) {
        Store store = null;
        if (isSalesUser) {
            store = storeService.checkOpStorePermission(storeLegal.getStoreId(), salesId);
        } else {
            store = storeService.findById(storeLegal.getStoreId());
        }
        boolean flag = StoreStatusNode.create.equals(store.getStatusNote())
                || StoreStatusNode.idCard.equals(store.getStatusNote());
        if (!flag) {
            return "请勿重复提交身份证信息";
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 修改商户信息
        Store mStore = new Store();
        mStore.setId(storeLegal.getStoreId());
        mStore.setStatusNote(StoreStatusNode.idCard);
        mStore.setStatus(StoreStatus.follow);
        storeService.updateById(mStore);

        // 保存身份证信息
        StoreLegal oldLegal = this.findStoreLegalByStoreId(store.getId());
        if (null == oldLegal) {
            // 保存身份证信息
            storeLegal.setCreateTime(new Date());
            storeLegal.setEnableFlag(XyrlConstant.YES);
            super.save(storeLegal);
            // 保存附件信息
            attachmentService.handleAttImageUseAttId(storeLegal.getId(),
                    AttImageUseType.idCardEmblem, storeLegal.getEmblemAttId(), null);
            attachmentService.handleAttImageUseAttId(storeLegal.getId(),
                    AttImageUseType.idCardHead, storeLegal.getHeadAttId(), null);
        } else {
            storeLegal.setId(oldLegal.getId());
            super.updateById(storeLegal);

            // 保存附件信息
            attachmentService.handleAttImageUseAttId(storeLegal.getId(),
                    AttImageUseType.idCardEmblem, storeLegal.getEmblemAttId(), oldLegal.getEmblemAttId());
            attachmentService.handleAttImageUseAttId(storeLegal.getId(),
                    AttImageUseType.idCardHead, storeLegal.getHeadAttId(), oldLegal.getHeadAttId());
        }

        return null;
    }
    @Override
    public StoreLegal findStoreLegalByStoreId(long storeId) {
        LambdaQueryWrapper<StoreLegal> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StoreLegal::getStoreId, storeId);
        wrapper.eq(StoreLegal::getEnableFlag, XyrlConstant.YES);
        return super.getOne(wrapper);
    }

}
